package com.ineatconseil.yougo.components;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.ineatconseil.yougo.beans.User;
import com.ineatconseil.yougo.pages.Index;
import com.ineatconseil.yougo.services.auth.Authenticator;

/**
 * Layout component for pages of application yougo-tapestry.
 */
@Import(stylesheet = { "context:layout/layout.css" })
public class Layout {

	/** The page title, for the <title> element and the <h1>element. */
	@SuppressWarnings("unused")
	@Property
	@Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
	private String title;

	@Property
	private String pageName;

	@Inject
	private ComponentResources resources;

	@Inject
	private Authenticator authenticator;

	public String getClassForPageName() {
		return resources.getPageName().equalsIgnoreCase(pageName) ? "current_page_item" : null;
	}

	public User getUser() {
		return authenticator.isLoggedIn() ? authenticator.getLoggedUser() : null;
	}

	@Log
	public Object onActionFromLogout() {
		authenticator.logout();
		return Index.class;
	}

}
